package support

import (
	"context"
	"time"

	"v8s-controller/generator"
	"v8s-controller/internal/configuration"
	"v8s-controller/internal/controller"
	"v8s-controller/internal/db"
	"v8s-controller/internal/k8s"
	"v8s-controller/test/support/helpers"
	"v8s-controller/test/support/kuberneteshelper"
	"v8s/test/containers/k3s"
	"v8s/test/containers/mongodb"
)

type (
	ApplicationContext struct {
		ProjectsController *controller.SynchronizationController
		Generator          *generator.Generator

		MongoHelper *helpers.MongoHelper
		K8SHelper   *kuberneteshelper.KubernetesHelper
	}
)

func GetApplicationContext(ctx context.Context, mongodb *mongodb.Container, k3s *k3s.Container) *ApplicationContext {
	mongoProperties := configuration.MongoProperties{
		ConnectionURI: mongodb.ConnectionURI(),
		Database:      "test-database",
	}
	taskProperties := configuration.TaskProperties{
		ProvisioningDeadline: time.Hour * 24 * 365 * 100,
	}
	generatorProperties := configuration.GeneratorProperties{
		Quota: configuration.NamespaceQuotaProperties{
			CPULimit:              "1000m",
			RAMLimit:              "1024Mi",
			EphemeralStorageLimit: "20Gi",
		},
	}

	mongoClient := db.CreateMongoClient(ctx, mongoProperties)
	databaseRepository := db.CreateProjectsRepository(mongoClient, mongoProperties)
	mongoHelper := helpers.CreateMongoHelper(mongoClient, mongoProperties)

	k8sClient := k3s.GetClient(ctx)
	k8sRepository := k8s.CreateRepository(k8sClient)
	k8sHelper := kuberneteshelper.CreateKubernetesHelper(k8sClient, k8sRepository)

	composeGenerator := generator.NewGenerator(generatorProperties)
	projectsController := controller.CreateSynchronizationController(databaseRepository, k8sRepository, composeGenerator, configuration.Properties{
		Task:      taskProperties,
		Generator: generatorProperties,
		Mongo:     mongoProperties,
		Feature:   configuration.FeatureProperties{LoggingEnabled: false},
	})

	return &ApplicationContext{
		ProjectsController: projectsController,
		Generator:          composeGenerator,

		MongoHelper: mongoHelper,
		K8SHelper:   k8sHelper,
	}
}
